
using TMPro;
using UnityEngine;
using UnityEngine.UI;

// 语言设置

public class LanguageSet : MonoBehaviour
{
    [SerializeField] private SetSelect setSelect;
    [SerializeField] private Button[] buttons;
    private int index;
    public bool set;
    private int language;

    [SerializeField] TextMeshProUGUI languageText;
    [SerializeField] private GameObject Tooptip;

    [SerializeField] private TextMeshProUGUI title;
    [SerializeField] private TextMeshProUGUI languageTextTitle;
    [SerializeField] private TextMeshProUGUI saveAndesc;
    [SerializeField] private TextMeshProUGUI exit;

    void Start()
    {

    }
    private void OnEnable()
    {
        language = setSelect.language;
    }

    void Update()
    {
        buttons[index].Select();

        if (!set)
        {
            LanguageSelect();
        }
        else
        {
            LanguageSetValue();
        }
        LanguageSetConfirm();
        SetText();
    }

    private void LanguageSetConfirm()
    {
        if (Tooptip.activeSelf) return;
        if (Input.GetKeyDown(KeyCode.J) || ComTrans.Instance.GetsecSerial() || ComTrans.Instance.GetsetSerial())
        {
            if (!set)
            {
                if (index >= 1)
                {
                    Tooptip.SetActive(true);
                    set = true;
                    switch (index)
                    {
                        case 1:
                            if (setSelect.language == 0)
                            {
                                Tooptip.GetComponent<ToolTip>().SetupToolTipText("是否保存并退出", "LanguageSave");
                            }
                            else
                            {
                                Tooptip.GetComponent<ToolTip>().SetupToolTipText("Do you want to save and exit", "LanguageSave");
                            }
                            break;
                        case 2:
                            if (setSelect.language == 0)
                            {
                                Tooptip.GetComponent<ToolTip>().SetupToolTipText("是否不保存退出", "LanguageDontSave");
                            }
                            else
                            {
                                Tooptip.GetComponent<ToolTip>().SetupToolTipText("Do you want to exit without saving", "LanguageDontSave");
                            }
                            break;
                    }
                    return;
                }

                if (0 == index)
                {
                    buttons[0].transform.GetChild(2).gameObject.SetActive(true);
                }
                else
                {
                    buttons[0].transform.GetChild(2).gameObject.SetActive(false);
                }

                set = true;
            }
            else
            {
                set = false;
                buttons[0].transform.GetChild(2).gameObject.SetActive(false);

            }
        }
    }

    // 选择修改
    private void LanguageSelect()
    {
        if (Tooptip.activeSelf) return;
        if (Input.GetKeyDown(KeyCode.Q) || ComTrans.Instance.GetHorizontalL() == -1)
        {
            if (--index < 0) index = buttons.Length - 1;
        }
        if (Input.GetKeyDown(KeyCode.E) || ComTrans.Instance.GetHorizontalR() == 1)
        {
            if (++index >= buttons.Length) index = 0;
        }

    }

    //修改值
    private void LanguageSetValue()
    {
        if (Tooptip.activeSelf) return;
        if (Input.GetKeyDown(KeyCode.Q) || ComTrans.Instance.GetHorizontalL() == -1)
        {
            if (index == 0)
            {
                setSelect.language--;
                if (setSelect.language < 0) setSelect.language = 1;
            }
        }
        if (Input.GetKeyDown(KeyCode.E) || ComTrans.Instance.GetHorizontalR() == 1)
        {
            if (index == 0)
            {
                setSelect.language++;
                if (setSelect.language > 1) setSelect.language = 0;
            }
        }
    }

    private void SetText()
    {
        if (setSelect.language == 0)
        {
            title.text = "语言设置";
            languageTextTitle.text = "游戏语言";
            saveAndesc.text = "保存并退出";
            exit.text = "不保存退出";
        }
        else
        {
            title.text = "Language settings";
            languageTextTitle.text = "Game language";
            saveAndesc.text = "Save and exit";
            exit.text = "Exit without saving";
        }
        languageText.text = setSelect.language == 1 ? "English" : "中文";
    }

    public void LanguageSave()
    {
        PlayerPrefs.SetInt("language", setSelect.language);
        PlayerPrefs.Save();
        index = 0;
        set = false;
        setSelect.ChangeSetUI(0);
        Debug.Log("保存语言");
    }

    public void LanguageDontSave()
    {
        setSelect.language = language;
        index = 0;
        set = false;
        setSelect.ChangeSetUI(0);
        Debug.Log("不保存语言");
    }

}
